#!/usr/bin/python
#Drew Carpenter & Jeff Gullett
#CS 4444
#Excavare 0.0
#March 29,2011
import os
import sys
import gtk
import pygtk
def save_file(self,widget):
		current=self.notebook.get_current_page()
		tm=self.treeview.get_model()
		iter=tm.get_iter(current)
		saved_once=tm.get_value(iter,5)
		file1=tm.get_value(iter,2)
		if file1 is not None:
			extType=file1.split(".")
			if extType[-1] != "txt":
				warning = gtk.MessageDialog(parent=self.excavare, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="You are trying to save a file of type "+extType[-1]+".\nExcavare cannot save this file type. If you would like to save what you have as a plain text file (.txt) press OK.\nThe original file has not been changed.")
				warn = warning.run()
				if warn ==gtk.RESPONSE_CANCEL:
					warning.destroy()
					return
				else:
					warning.destroy()
		if saved_once is False:
			filesel = gtk.FileChooserDialog(title='Save',parent=self.excavare,action=gtk.FILE_CHOOSER_ACTION_SAVE,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK))
			filesel.set_default_response(gtk.RESPONSE_OK)
			file_action=filesel.run()
			if file_action==gtk.RESPONSE_OK:
				scrolly=self.notebook.get_focus_child()
				view=scrolly.get_child()
				texty=view.get_child()
				buffy=texty.get_buffer()
				start=buffy.get_start_iter()
				end=buffy.get_end_iter()
				all_text=buffy.get_text(start,end)
				name=filesel.get_filename()
				extension=name.split(".")
				if len(extension)==1:
					temp_name=name+".txt"
				if extension[-1]!=".txt":
					temp_name=extension[0]+".txt"
				else:
					temp_name=name
				finalNameList=temp_name.split("/")
				if len(finalNameList)==1:
					finalNameList=temp_name.split("\\")
				finalName=finalNameList[-1]
				if os.path.exists(finalName):
					warning = gtk.MessageDialog(parent=filesel, flags=0, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK_CANCEL, message_format="The file already exists. Are you sure you want to repace it?")
					warn = warning.run()
					if warn ==gtk.RESPONSE_CANCEL:
						warning.destroy()
						return
					else:
						#erase the file
						erase = open(finalName,"w").close()
						warning.destroy()
				filesel.destroy()
				new_file = open(finalName,"w")
				new_file.write(all_text)
				new_file.close()
				current=self.notebook.get_current_page()
				tm=self.treeview.get_model()
				iter=tm.get_iter(current)
				tm.set_value(iter,0,finalName)
				tm.set_value(iter,2,finalName)
				tm.set_value(iter,5,True)
				new_label=gtk.Label(finalName)
				self.notebook.set_tab_label(scrolly,new_label)
			if file_action == gtk.RESPONSE_CANCEL:
				#if cancel is clicked, destroy file chooser dialog
				filesel.destroy()
		else:
			scrolly=self.notebook.get_focus_child()
			view=scrolly.get_child()
			texty=view.get_child()
			buffy=texty.get_buffer()
			start=buffy.get_start_iter()
			end=buffy.get_end_iter()
			all_text=buffy.get_text(start,end)
			finalName=tm.get_value(iter,2)
			new_file = open(finalName,"w")
			new_file.write(all_text)
			new_file.close()